package com.ptp.backendcertms.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ptp.backendcertms.api.external.dto.req.QueryResultDetailReqDto;
import com.ptp.backendcertms.api.external.dto.req.QueryResultListReqDto;
import com.ptp.backendcertms.api.external.dto.res.PageInfoDto;
import com.ptp.backendcertms.api.external.dto.res.QueryResultListDto;
import com.ptp.backendcertms.api.external.dto.res.QueryResultListResDto;
import com.ptp.backendcertms.api.internal.constant.BusinessTypeEnum;
import com.ptp.backendcertms.basiccompnents.data.dao.entity.CertificatePo;
import com.ptp.backendcertms.basiccompnents.data.dao.entity.ReportPo;
import com.ptp.backendcertms.basiccompnents.data.dao.service.ICertificateService;
import com.ptp.backendcertms.basiccompnents.data.dao.service.IReportService;
import com.ptp.backendcertms.basiccompnents.properties.ProjectProperties;
import com.ptp.backendcertms.business.handler.PageInfoCommonHandler;
import com.ptp.backendcertms.business.service.ICertificateBusinessService;
import com.ptp.backendcertms.business.service.IOrganizationQueryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class OrganizationQueryServiceImpl implements IOrganizationQueryService {

    @Resource
    private ICertificateService certificateService;
    @Resource
    private IReportService reportService;
    @Resource
    private PageInfoCommonHandler pageInfoCommonHandler;
    @Resource
    private ICertificateBusinessService certificateBusinessService;
    @Resource
    private ProjectProperties projectProperties;


    @Override
    public QueryResultListResDto list(QueryResultListReqDto reqDto) {
        //判断是证书查询还是报告查询
        QueryResultListResDto resDto = new QueryResultListResDto();
        resDto.setPageInfo(pageInfoCommonHandler.buildPageInfo(0L, reqDto.getCurrent(), reqDto.getPageSize()));
        if (StringUtils.isNotBlank(reqDto.getOrganizationName()) && Arrays.stream(projectProperties.getTemplate().getOrgNameShield()).anyMatch(t -> reqDto.getOrganizationName().equals(t))) {
            return resDto;
        }
        if (BusinessTypeEnum.certificate.name().equals(reqDto.getQueryType())) {
            return queryCertificate(reqDto);
        } else if (BusinessTypeEnum.report.name().equals(reqDto.getQueryType())) {
            return queryReport(reqDto);
        }
        return resDto;
    }

    @Override
    public Map<String, Object> detail(QueryResultDetailReqDto reqDto) {
        Object dto = null;
        if (BusinessTypeEnum.certificate.name().equals(reqDto.getQueryType())) {
            dto = certificateBusinessService.getCertificateDetail(reqDto.getSerialNo());
        }
        if(Objects.isNull(dto)){
            return Collections.emptyMap();
        }
        return BeanUtil.beanToMap(dto);
    }


    private QueryResultListResDto queryCertificate(QueryResultListReqDto reqDto) {
        CertificatePo queryPo = new CertificatePo();
        queryPo.setCertificateNo(reqDto.getBusinessNo());
        queryPo.setOrganizationName(reqDto.getOrganizationName());
        IPage<CertificatePo> pageList = certificateService.pageList(queryPo, reqDto.getCurrent(), reqDto.getPageSize());
        // 分页信息
        PageInfoDto pageInfo = pageInfoCommonHandler.buildPageInfo(pageList.getTotal(), reqDto.getCurrent(), reqDto.getPageSize());
        QueryResultListResDto resDto = new QueryResultListResDto();
        resDto.setPageInfo(pageInfo);
        if (pageInfo.getTotal() <= 0) {
            return resDto;
        }
        List<QueryResultListDto> list = pageList.getRecords().stream().map(this::wrapperCertificateDto).collect(Collectors.toList());
        resDto.setList(list);
        return resDto;
    }

    private QueryResultListResDto queryReport(QueryResultListReqDto reqDto) {
        ReportPo queryPo = new ReportPo();
        queryPo.setReportNo(reqDto.getBusinessNo());
        queryPo.setOrganizationName(reqDto.getOrganizationName());
        IPage<ReportPo> pageList = reportService.pageList(queryPo, reqDto.getCurrent(), reqDto.getPageSize());
        // 分页信息
        PageInfoDto pageInfo = pageInfoCommonHandler.buildPageInfo(pageList.getTotal(), reqDto.getCurrent(), reqDto.getPageSize());
        QueryResultListResDto resDto = new QueryResultListResDto();
        resDto.setPageInfo(pageInfo);
        if (pageInfo.getTotal() <= 0) {
            return resDto;
        }
        List<QueryResultListDto> list = pageList.getRecords().stream().map(this::wrapperReportDto).collect(Collectors.toList());
        resDto.setList(list);
        return resDto;
    }


    private QueryResultListDto wrapperCertificateDto(CertificatePo po) {
        QueryResultListDto resDto = new QueryResultListDto();
        resDto.setSerialNo(po.getSerialNo());
        // 编号-证书编号
        resDto.setBusinessNo(po.getCertificateNo());
        resDto.setOrganizationName(po.getOrganizationName());
        resDto.setResultTypeCode(BusinessTypeEnum.certificate.name());
        resDto.setResultTypeName(BusinessTypeEnum.certificate.getDesc());
        resDto.setDate(po.getDueDate());
        resDto.setResultName(po.getCertificationProject());
        resDto.setStatus(po.getCertificateStatus().name());
        resDto.setStatusDesc(po.getCertificateStatus().getValue());
        return resDto;
    }

    private QueryResultListDto wrapperReportDto(ReportPo po) {
        QueryResultListDto resDto = new QueryResultListDto();
        resDto.setSerialNo(po.getSerialNo());
        // 编号-报告编号
        resDto.setBusinessNo(po.getReportNo());
        resDto.setOrganizationName(po.getOrganizationName());
        resDto.setResultTypeCode(BusinessTypeEnum.report.name());
        resDto.setResultTypeName(BusinessTypeEnum.report.getDesc());
        resDto.setDate(po.getReleaseTime());
        resDto.setResultName(po.getReportName());
        return resDto;
    }
}
